﻿using System.Collections.Generic;
using System.Linq;
using lindyaka.Repository.Entities;
using lindyaka.Repository.Interfaces;

namespace lindyaka.Services.Administration
{
    public class UserAccounts
    {
        private readonly IRoleRepository _repositoryRoles;
        private readonly IUserAccountRoleRepository _repositoryUserAccountRoles;
        private readonly IUserAccountRepository _repositoryUserAccounts;

        public UserAccounts() : this(new UserAccountRole(), new UserAccount(), new Role())
        {
        }

        public UserAccounts(IUserAccountRoleRepository repositoryUserAccountRoles,
                            IUserAccountRepository repositoryUserAccounts, IRoleRepository repositoryRoles)
        {
            _repositoryUserAccountRoles = repositoryUserAccountRoles;
            _repositoryUserAccounts = repositoryUserAccounts;
            _repositoryRoles = repositoryRoles;
        }

        public IEnumerable<Models.Administration.UserAccount> Get()
        {
            return (from u in _repositoryUserAccounts.FindAll()
                    select new Models.Administration.UserAccount
                        {
                            Email = u.Email,
                            LastLoggedIn = u.LastLoggedIn,
                            Password = u.Password,
                            Permission = u.Permission,
                            Registered = u.Registered,
                            Status = u.Status,
                            UserAccountId = u.UserAccountId,
                            Roles =
                                (from d in
                                     _repositoryUserAccountRoles.FindMany(
                                         userAccountRole => userAccountRole.UserAccountId == u.UserAccountId)
                                 join r in _repositoryRoles.FindAll()
                                     on d.UserAccountRoleId equals r.RoleId
                                 select r)
                        }).AsEnumerable();
        }

        public Models.Administration.UserAccount Get(int userAccountId)
        {
            Domain.Entities.UserAccount userAccount = _repositoryUserAccounts.Find(u => u.UserAccountId == userAccountId);
            return new Models.Administration.UserAccount
                {
                    Email = userAccount.Email,
                    LastLoggedIn = userAccount.LastLoggedIn,
                    Password = userAccount.Password,
                    Permission = userAccount.Permission,
                    Registered = userAccount.Registered,
                    Status = userAccount.Status,
                    UserAccountId = userAccount.UserAccountId
                };
        }

        public void Add(Models.Administration.UserAccount userAccount)
        {
            _repositoryUserAccounts.Add(new Domain.Entities.UserAccount
                {
                    Email = userAccount.Email,
                    LastLoggedIn = userAccount.LastLoggedIn,
                    Password = userAccount.Password,
                    Permission = userAccount.Permission,
                    Registered = userAccount.Registered,
                    Status = userAccount.Status
                });
        }

        public void Update(Models.Administration.UserAccount userAccount)
        {
            _repositoryUserAccounts.Update(new Domain.Entities.UserAccount
                {
                    Email = userAccount.Email,
                    LastLoggedIn = userAccount.LastLoggedIn,
                    Password = userAccount.Password,
                    Permission = userAccount.Permission,
                    Registered = userAccount.Registered,
                    Status = userAccount.Status,
                    UserAccountId = userAccount.UserAccountId
                });
        }

        public void Remove(Models.Administration.UserAccount userAccount)
        {
            _repositoryUserAccounts.Remove(u => u.UserAccountId == userAccount.UserAccountId);
        }
    }
}